/* jshint esversion: 6 */
(function () {
    // 核心样式管理
    PetUI.Style = function () {
        $.Style = null;
        // style
        let style = document.createElement('style');
        document.head.appendChild(style);
        // 样式列表
        let stylelist = {};
        let StyleUpdate = () => {
            for (let key in stylelist) {
                if (stylelist.hasOwnProperty(key)) {
                    text += key + "{ \n";
                    let css = stylelist[key];
                    if (typeof css == "string") {
                        text += css;
                    } else {
                        for (let i = 0; i < css.length; i++) {
                            text += css[i] + ";\n";
                        }
                    }
                    text += "}\n";
                }
            }
            style.innerText = text;
        };
        // 样式修改
        Object.defineProperty($, "Style", {
            set: function (name) {
                // CSS 由 选择器 { 声明列表 } 构成
                if (name.selector) {
                    if (typeof name.statement == "string") {
                        stylelist[name.selector] = name.statement;
                    } else {
                        if (name.statement && name.statement.length > 0) {
                            stylelist[name.selector] = name.statement;
                        }
                    }
                    StyleUpdate();
                }
            },
            get: function () {
                let $ = function (selector, statement) {
                    if (statement != undefined && statement != undefined) {
                        $.selector = selector;
                        $.statement = statement;
                        $.Update();
                    } else if (typeof selector == "string" && stylelist[selector]) {
                        $.selector = name;
                        $.statement = stylelist[name];
                        return $;
                    }
                };
                $.css = "";
                $.selector = null;
                $.statement = [];
                $.stylelist = stylelist;
                $.Update = () => {
                    if ($.selector) {
                        if (typeof $.statement == "string") {
                            stylelist[$.selector] = $.statement.split(";");
                        } else {
                            if ($.statement && $.statement.length > 0) {
                                stylelist[$.selector] = $.statement;
                            }
                        }
                        StyleUpdate();
                    }
                };
                $.StyleUpdate = StyleUpdate;
                Object.defineProperty($, "css", {
                    set: function (name) {
                        $.statement.push(name);
                    }
                });
                return $;
            }
        });
        return $;
    };
    // 提示
    var Frame = PetUI.Create('div');
    Frame.classList = "Frame";
    Frame.Initialize = true;
    PetUI.Prompt = (notice, t) => {
        if (notice) {
            var div = Frame.Create('div');
            div.innerHTML = notice;
            window.setTimeout(() => {
                div.Destroy();
            }, t ? t : 5000);
            // 返回底层
            if (Frame.Initialize) {
                Frame.Child();
                Frame.Initialize = false;
            }
            return div;
        } else {
            return Frame;
        }
    };
    PetUI.PromptErr = (notice, t) => {
        var err = PetUI.Prompt(notice, t);
        err.CSS("text-align: left");
        err.classList = "Red";
    }
    //------------- UI -------------
    //-> 按钮
    PetUI.Button = (text) => {
        var button = PetUI.Create('button');
        // 设置样式
        button.classList = "PetUIButton";
        // 设置事件
        button.Click = null;
        button.UI.Click = (e) => {
            if (button.Click != undefined) {
                button.Click();
            }
        };
        if (text) {
            button.innerText = text;
        }
        return button;
    };
    // 按钮颜色样式
    PetUI.Button.style = [
        // 背景颜色
        "Red",
        "Blue",
        "Green",
        "Gray",
        "Black",
        // 点燃颜色
        "HoverRed",
        "HoverBlue",
        "HoverGreen",
        "HoverGray",
        "HoverBlack",
        // 按下颜色
        "ActiveRed",
        "ActiveBlue",
        "ActiveGreen",
        "ActiveGray",
        "ActiveBlack"
    ];
    // 按钮动画
    PetUI.Button.styleAnimation = {
        "Arrows": "<span>按钮动画</span>"
    };
    //-> 选择按钮
    PetUI.Selection = (text) => {
        var div = PetUI.Create('div');
        div.classList = 'PetUISwitch CloseGlowing';
        // 复选框
        var input = div.Create('input');
        input.classList = 'input';
        input.type = 'checkbox';
        div.input = input;
        // 滑块
        var slider = div.Create('label');
        slider.classList = 'slider';
        div.slider = slider;
        // 名称
        if (text) {
            var label = div.Create('label');
            label.classList = 'label';
            label.innerText = text;
            div.label = label;
        }
        // 状态
        div.Checked = false;
        Object.defineProperty(div, "Checked", {
            set: function (name) {
                input.checked = name;
            },
            get: function () {
                return input.checked;
            }
        });
        // 事件设置
        div.Click = null;
        slider.UI.Click = (e) => {
            div.Checked = !input.checked;
            if (div.Click) {
                div.Click(div, e);
            }
        };
        return div;
    };
    // 选择按钮样式
    PetUI.Selection.style = [
        "primary",
        "info",
        "success",
        "danger",
        "warning"
    ];
    // 选择按钮效果样式
    PetUI.Selection.styleEffect = [
        "CloseGlowing"
    ];
    //-> 进度条
    PetUI.ProgressBar = (width, percent) => {
        var div = PetUI.Create('div');
        div.classList = "PetUIProgressBar CloseGlowing";
        // 进度条背景
        var Background = div.Create("div");
        Background.classList = "PetUIProgressBar background";
        div.Background = Background;
        // 进度条颜色
        var Colop = Background.Create("div");
        Colop.classList = "PetUIProgressBar color";
        div.Colop = Colop;
        // 进度条大小
        div.CSS({
            width: width ? width : '200px'
        });
        // 进度条百分比
        div.__Percent = percent ? percent : 0;
        Colop.CSS({
            'width': percent ? percent + '%' : "1px"
        });
        var lock = false;
        div.Percent = (percent) => {
            if (lock == true) {
                return;
            }
            if (percent) {
                if (div.__Percent != percent) {
                    lock = true;
                    if (div.__Percent > percent) {
                        let n = div.__Percent - percent;
                        let id = setInterval(() => {
                            n--;
                            Colop.CSS({
                                'width': percent + n + '%'
                            });
                            if (n == 0) {
                                lock = false;
                                div.__Percent = percent;
                                clearTimeout(id);
                            }
                        }, 20);
                    } else {
                        let n = div.__Percent;
                        let id = setInterval(() => {
                            n++;
                            Colop.CSS({
                                'width': n + '%'
                            });
                            if (n == percent) {
                                lock = false;
                                div.__Percent = percent;
                                clearTimeout(id);
                            }
                        }, 20);
                    }
                    return div;
                }
            }
            return div.__Percent;
        };
        div.Percent(percent);
        // 进度条样式
        div.ClassColop = (...e) => {
            Colop.Class(...e);
        };
        return div;
    };
    // 进度条样式
    PetUI.ProgressBar.style = [
        'color-1',
        'color-2',
        'color-3',
        'color-4',
        'color-5',
        'color-6',
        'color-7',
        'color-8'
    ];
    PetUI.ProgressBar.styleEffect = [
        "BackgroundAnime",
        "CloseGlowing"
    ];
    //-> 圆形进度条
    PetUI.ProgressBarRound = (width) => {
        var div = PetUI.Create('div');
        div.classList = "PetUIProgressBar Round";
        div.Create("i");
        if (width) {
            div.CSS({
                "font-size": width + "px"
            });
        }
        return div;
    };
    // 导航栏
    PetUI.Navbar = () => {
        var div = PetUI.Create('div');
        div.classList = "Navbar Description";
        // * 设置导航栏按钮
        div.Submenu = (text) => {
            var vessel = div.Create('div');
            var menu = vessel.Create('div');
            menu.classList = "NavbarButton";
            menu.innerText = text;
            // 创建子菜单
            var submenu = PetUI.ContextNavbar();
            submenu.classList += " Description";
            vessel.appendChild(submenu);
            // * 得到菜单
            menu.GetSubmenu = function () {
                return submenu;
            };
            // 设置事件
            menu.Click = null;
            menu.UI.Click = (e) => {
                // * 显示菜单
                if (submenu.childNodes.length > 0) {
                    submenu.Display(true);
                }
                // *  触发点击事件
                if (menu.Click != undefined) {
                    menu.Click();
                }
            };
            return menu;
        };
        return div;
    };
    // 菜单
    PetUI.ContextNavbar = () => {
        var div = PetUI.Create('div');
        div.Display(false);
        div.classList = 'ContextMenu';
        // 子菜单
        var Submenu = function (text) {
            var menu = this.Create('div');
            menu.classList = 'ContextTitle';
            (menu.Text = menu.Create("a")).innerText = text;
            // 设置事件
            menu.Click = null;
            menu.UI.Click = (e) => {
                if (menu.Click != undefined) {
                    menu.Click();
                }
            };
            // 子菜单
            menu.Submenu = Submenu;
            return menu;
        };
        div.Autohide = true;
        // 设置隐藏
        PetUI.Click = (e) => {
            if (div.Autohide) {
                div.Display(false);
            }
        };
        div.Submenu = Submenu;
        div.AddSplitBar = function () {
            this.Create('hr');
        };
        return div;
    };
    // 页码
    PetUI.Pagination = (number) => {
        var ul = PetUI.Create("ul");
        ul.classList = "Pagination";
        ul.CurrentPage = 0; // 当前页
        ul.Click = null;
        ul.Number = 0; // 数量
        // 设置页 id 第几页
        ul.Selected = function (id) {
            if (id > 0 && id <= ul.Number) {
                for (var i = 0; i < ul.children.length; i++) {
                    var but = ul.children[i].children[0];
                    if (but.id == id) {
                        but.classList = "Active";
                        // 设置当前页
                        ul.CurrentPage = id - 1;
                        if (ul.Click != undefined) {
                            ul.Click(ul.CurrentPage);
                        }
                    } else {
                        but.classList = "";
                    }
                }
            }
        };
        // 点击事件
        ul.UI.Click = (e) => {
            if (e.target.innerText == "<<") {
                ul.Selected(ul.CurrentPage);
            } else if (e.target.innerText == ">>") {
                let i = ul.CurrentPage + 2;
                ul.Selected(i);
            } else {
                ul.Selected(e.target.id);
            }
        };
        // 设置页数量 number 页码数量
        ul.SetPagination = function (number) {
            // 初始化
            var HTML = "";
            HTML += "<li><button><<</button></li>";
            for (var i = 1; i < number + 1; i++) {
                HTML += '<li><button class="" id=' + i + '>' + i + "</button></li>";
            }
            HTML += "<li><button>>></button></li>";
            // 设置默认
            ul.Number = number;
            ul.innerHTML = HTML;
            ul.CurrentPage = 0;
            ul.Selected(1);
        };
        // 设置默认
        ul.SetPagination(number ? number : 0);
        return ul;
    };
    //  日历
    PetUI.Calendar = function () {
        /**
         * 判断某年某月的1号是星期几
         * @param {*} year  年
         * @param {*} month  月
         */
        this.GetFirstDay = function (year, month) {
            var allDay = 0,
                y = year - 1,
                i = 1;
            allDay = y + Math.floor(y / 4) - Math.floor(y / 100) + Math.floor(y / 400) + 1;
            for (; i < month; i++) {
                switch (i) {
                    case 1:
                        allDay += 31;
                        break;
                    case 2:
                        if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
                            allDay += 29;
                        } else {
                            allDay += 28;
                        }
                        break;
                    case 3:
                        allDay += 31;
                        break;
                    case 4:
                        allDay += 30;
                        break;
                    case 5:
                        allDay += 31;
                        break;
                    case 6:
                        allDay += 30;
                        break;
                    case 7:
                        allDay += 31;
                        break;
                    case 8:
                        allDay += 31;
                        break;
                    case 9:
                        allDay += 30;
                        break;
                    case 10:
                        allDay += 31;
                        break;
                    case 11:
                        allDay += 30;
                        break;
                    case 12:
                        allDay += 31;
                        break;
                }
            }
            return allDay % 7;
        };
        /**
         * 获得某月日期
         * @param {*} year   年
         * @param {*} month  月
         * @param {*} flag   标记
         */
        this.GetDate = function (year, month, flag) {
            if (typeof year === "undefined" && typeof month === "undefined") {
                var myDate = new Date();
                year = myDate.getFullYear();
                month = myDate.getMonth() + 1;
                flag = myDate.getDate();
            }
            // 处理上一个月份
            var number_b = 0;
            if (month != 1) {
                number_b = new Date(year, month - 1, 0).getDate();
            } else {
                number_b = new Date(year - 1, 12, 0).getDate();
            }
            var day = this.GetFirstDay(year, month);
            var week = [];
            for (var i = 0; i < day - 1; i++) {
                week.push("<div Highlight = false>" + parseInt(number_b - i) + "</div>");
            }
            week.reverse();
            // 处理当前月份
            var number_a = new Date(year, month, 0).getDate();
            for (i = 1; i < number_a + 1; i++) {
                week.push("<div Highlight = " + (flag != i ? "true" : "high") + ">" + i + "</div>");
            }
            // 下一个月份
            var n = week.length;
            for (i = 1; n < 42; i++, n++) {
                week.push("<div Highlight = false>" + i + "</div>");
            }
            // 处理数据
            var html = '<div class="Calendar Headline">' + '<div>' + year + "年" + month + "月</div><div>" + '<div>一</div><div>二</div><div>三</div><div>四</div><div>五</div><div>六</div><div>日</div></div></div>';
            for (n = 0; n < 42; n += 7) {
                html += "<div>" + week.slice(n, n + 7).join("") + "</div>";
            }
            var div = PetUI.Create("div");
            div.classList = "Calendar";
            div.innerHTML = html;
            return div;
        };
    };
    // TabStrip 控件
    PetUI.TabStrip = function () {
        var $ = this;
        // 容器列表
        this.ContainerList = {};
        // 添加容器
        // name 名称
        // obj  容器对象
        this.SetContainer = function (name, obj) {
            if (obj.UI != undefined) {
                return $.ContainerList[name] = obj;
            }
        };
        // 设置容器状态
        //  name  名称
        //  state 状态 隐藏(true)/显示(false)
        this.SetState = function (name, state) {
            // *  隐藏全部
            for (const key in $.ContainerList) {
                if (name == key) {
                    $.ContainerList[name].Display(state);
                } else {
                    $.ContainerList[key].Display(false);
                }
            }
            // * 设置目标容器状态
            return $.ContainerList[name]
        };
    };
})();